package com.hy.frame.util

import android.content.Context
import com.hy.frame.R
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * title 时间工具
 * author heyan
 * time 19-3-11 下午6:52
 * desc 无
 */
object DateUtil {
    /**
     * 获取当前时间Date "yyyy-MM-dd HH:mm:ss"
     * @return 现在时间(Now)
     */
    val nowTime: String
        get() = getNowTime("yyyy-MM-dd HH:mm:ss")

    /**
     * 获取当前时间Date "yyyy-MM-dd HH:mm:ss"
     * @param type 指定格式
     * @return 现在时间(Now)
     */
    fun getNowTime(type: String?): String {
        var ttype = type
        val d = Date(System.currentTimeMillis())
        // String type = "yyyy-MM-dd HH:mm:ss";
        if (ttype == null)
            ttype = "HH:mm:ss"
        val formatter = SimpleDateFormat(ttype, Locale.CHINA)
        return formatter.format(d)
    }

    /**
     * 获取当前时间Date
     */
    fun getDateTime(date: Long, type: String? = null): String {
        var time = date
        var ttype = type
        if ((time.toString() + "").length == 10) time *= 1000L
        if (ttype == null) ttype = "yyyy-MM-dd HH:mm:ss"
        val d = Date(time)
        val formatter = SimpleDateFormat(ttype, Locale.CHINA)
        return formatter.format(d)
    }

    fun stringToDateTime(strDate: String?, type: String? = null): Date? {
        var t = type
        if (strDate != null) {
            if (type == null) t = "yyyy-MM-dd HH:mm:ss"
            try {
                val sdf = SimpleDateFormat(t, Locale.CHINA)
                return sdf.parse(strDate)
            } catch (e: ParseException) {
                e.printStackTrace()
            }

        }
        return null
    }

    fun getNowMinutes(): Int {
        val cal = Calendar.getInstance()
        cal.timeInMillis = System.currentTimeMillis()
        val hour = cal.get(Calendar.HOUR_OF_DAY)
        val minute = cal.get(Calendar.MINUTE)
        return hour * 60 + minute
    }

    fun getNowMinutes(str: String?): Int {
        if (str.isNullOrEmpty() || !str!!.contains(":")) return 0
        val strs = str!!.split(":")
        return strs[0].toInt() * 60 + strs[1].toInt()
    }

    /**
     * 获取周几
     * @param week
     */
    fun getWeekName(week: Int, context: Context): String {
        when (week) {
            java.util.Calendar.SUNDAY -> context.getString(R.string.sunday)
            java.util.Calendar.MONDAY -> context.getString(R.string.monday)
            java.util.Calendar.TUESDAY -> context.getString(R.string.tuesday)
            java.util.Calendar.WEDNESDAY -> context.getString(R.string.wednesday)
            java.util.Calendar.THURSDAY -> context.getString(R.string.thursday)
            java.util.Calendar.FRIDAY -> context.getString(R.string.friday)
            java.util.Calendar.SATURDAY -> context.getString(R.string.saturday)
        }
        return context.getString(R.string.empty)
    }
}